package com.byght.bygsystem.controller;

import com.byght.bygsystem.model.Personnel;
import com.byght.bygsystem.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/personnel")
@CrossOrigin(origins = "http://localhost:8080") // 允许前端访问的地址
public class PersonnelController {

    @Autowired
    private PersonnelService personnelService;

    @GetMapping
    public ResponseEntity<List<Personnel>> getAllPersonnel() {
        List<Personnel> personnelList = personnelService.getAllPersonnel();
        return ResponseEntity.ok(personnelList);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Personnel> getPersonnelById(@PathVariable int id) {
        Personnel personnel = personnelService.getPersonnelById(id);
        return ResponseEntity.ok(personnel);
    }

    @PostMapping
    public ResponseEntity<Void> addPersonnel(@RequestBody Personnel personnel) {
        personnelService.addPersonnel(personnel);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updatePersonnel(@RequestBody Personnel personnel) {
        personnelService.updatePersonnel(personnel);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePersonnel(@PathVariable int id) {
        personnelService.deletePersonnel(id);
        return ResponseEntity.ok().build();
    }

}
